﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Security.Cryptography;

namespace IRCTest
{
    /*
     * Several basic IRC commands which are ready to be used.
     * Upon instantiation the streamreader and writer have to be passed on which these commands must be executed
     * 
     * Send_Message is able to sent messages to the a) default channel , b) specific channel/user, c) ChanServ or NickServ
     */
    public delegate void BotMessageSend(object IRCBotInstance, MessageSendEvent Event);

    public abstract class IRCBasic
    {
        internal event BotMessageSend onMessageSend;
        private static Random Cnt;
        public static StreamReader Reader;
        public static StreamWriter Writer;

        private String channel;
        public String Channel { get { return channel; } set { channel = value; } } 
        

        public void Send_Message(String Message)
        {
            Writer.WriteLine("PRIVMSG " + Channel + " :\u000304{0}", Message);
            FireEvent(Message, Channel);
        }
        public void Send_Message2(String Message,String Receiver)
        {
            Writer.WriteLine("PRIVMSG " + Receiver + " :\u000304{0}", Message);
            FireEvent(Message, Receiver);
        }
        public void Send_Message3(String Message, String Command = "IDENTIFY", String Receiver = "NickServ")
        {
            Writer.WriteLine("PRIVMSG {0} {1} {2}", Receiver, Command, Message);
            FireEvent(Message, Receiver,Command);
        }
        public void Inviter(String Channel, String Person)
        {
            Writer.WriteLine("INVITE " + Person + " " + Channel);
            FireEvent(Person,Channel,"INVITE");
        }
        
        public void Notice(String Message, String Person)
        {
            Writer.WriteLine("NOTICE " + Person + " :{0}", Message);
            FireEvent(Message, Person);
        }

       
        public void Emote(String Emote, String Receiver)
        {
            Writer.WriteLine(String.Concat("PRIVMSG ", Receiver, " :", (char)1, "ACTION " , Emote, (char)1));
            FireEvent(Emote, Receiver,"EMOTE");
        }
        public void Slap(String Person,String Receiver)
        {
            
            String[] SlapItems = { "a pillow", "rotten eggs", "a large trout","a rubber chicken","a cup of hot tea.. Oops ! Spilled some on you .." };
            if (IRCBasic.Cnt == null)
            IRCBasic.Cnt = new Random(SlapItems.Length);
            Emote(String.Concat("slaps ", Person, " around with ", SlapItems[Cnt.Next(SlapItems.Length)], "!"),Receiver);
        }
        protected void set_Ident(String Ident)
        {
            Writer.WriteLine(Ident);
        }
        protected void set_Nick(String Nick)
        {
            Writer.WriteLine("NICK " + Nick);
        }
        public void Join_Chan(String Channel)
        {
            Writer.WriteLine("JOIN " + Channel);
        }
        protected void Mode(String Person,Char Sign,Char Option)
        {
            Writer.WriteLine(String.Concat("MODE ", Channel , " " ,  Sign , Option ," " ,Person));
        }
        public void Kick(String Person, String Channel,String Reason)
        {
            Writer.WriteLine(String.Concat("KICK ", Channel, " ", Person, " ", Reason));
        }

        public void ListUsers(String Channel)
        {
            Writer.WriteLine(String.Concat("NAMES ", Channel));
        }
        private void FireEvent(String Message,String Target,String Command = null)
        {
            if (onMessageSend != null)
                if (Command == null)
                    onMessageSend(this, new MessageSendEvent(Message, Target));
                else
                    onMessageSend(this, new MessageSendEvent(Message, Target, Command));
        }
    }
}
